﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Secure;
using Eassets.Service;
using Eassets.Layered.Domain.Secure;
using System.Collections.ObjectModel;
using System.Collections;
using Eassets.Layered.Domain;

namespace Eassets.UI.Secure
{
    public partial class UserPermissionForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetSecurityGroups();

                GetUnmappedUsers();
                GetMappedSecurityGroupUserBySecurityGroup();
            }
        }

        private void GetSecurityGroups()
        {
            dropDownListSecurityGroup.Items.Clear();
            SecurityGroupService securityGroupService = new SecurityGroupService();

            try
            {
                dropDownListSecurityGroup.DataSource =
                    securityGroupService.GetAllSecurityGroups();
                dropDownListSecurityGroup.DataValueField = "SecurityGroupId";
                dropDownListSecurityGroup.DataTextField = "Description";
                dropDownListSecurityGroup.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void GetUnmappedUsers()
        {
            if (dropDownListSecurityGroup.SelectedIndex > -1)
            {
                SecurityGroupUserService securityGroupUserService = new SecurityGroupUserService();
                List<long> empIdList = new List<long>();
                empIdList =
                    securityGroupUserService.GetUnMappedEmpIdsBySecurityGroup(
                        Convert.ToInt64(dropDownListSecurityGroup.SelectedItem.Value)).ToList();

                EmployeeService employeeService = new EmployeeService();
                List<Employee> employeesList = new List<Employee>();
                List<Employee> employeesListMain = new List<Employee>();
                foreach (long id in empIdList)
                {
                    Employee employee = new Employee();
                    employee = employeeService.GetByIdEmployee(id);

                    employeesList.Add(employee);
                }
                employeesListMain = employeesList.Where(x => x.EmployeeType == "Employee").ToList();
                gridViewAllStakeholders.DataSource = employeesListMain;
                gridViewAllStakeholders.DataBind();

            }
            else
            {
                gridViewAllStakeholders.DataSource = null;
                gridViewAllStakeholders.DataBind();
            }

        }

        private void GetMappedSecurityGroupUserBySecurityGroup()
        {
            if (dropDownListSecurityGroup.SelectedIndex > -1)
            {
                SecurityGroupUserService securityGroupUserService = new SecurityGroupUserService();
                List<long> empIdList = new List<long>();

                empIdList =
                    securityGroupUserService.GetEmpIdFromSecurityGroupUser(
                        Convert.ToInt64(dropDownListSecurityGroup.SelectedItem.Value)).ToList();

                EmployeeService employeeService = new EmployeeService();
                List<Employee> employeesList = new List<Employee>();
                foreach (long id in empIdList)
                {
                    Employee employee = new Employee();
                    employee = employeeService.GetByIdEmployee(id);

                    employeesList.Add(employee);
                }
                gridViewMappedStakeholders.DataSource = employeesList;
                gridViewMappedStakeholders.DataBind();
            }
            else
            {
                gridViewMappedStakeholders.DataSource = null;
                gridViewMappedStakeholders.DataBind();
            }

        }

        protected void dropDownListSecurityGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetUnmappedUsers();
            GetMappedSecurityGroupUserBySecurityGroup();
        }

        protected void buttonAddRight_Click(object sender, EventArgs e)
        {
            SecurityGroupUserService securityGroupUserService = new SecurityGroupUserService();
            Collection<SecurityGroupUser> securityGroupUsers = new Collection<SecurityGroupUser>();
            SecurityGroupUser securityGroupUser;
            Boolean isSelected = false;

            foreach (GridViewRow gridViewRow in gridViewAllStakeholders.Rows)
            {
                if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                {
                    securityGroupUser = new SecurityGroupUser();
                    securityGroupUser.SecurityGroupId = Convert.ToInt64(dropDownListSecurityGroup.SelectedItem.Value);
                    securityGroupUser.UserId = Convert.ToInt64(gridViewRow.Cells[1].Text);
                    securityGroupUser.EffectiveDate = DateTime.Today;

                    securityGroupUsers.Add(securityGroupUser);
                    isSelected = true;
                }
            }
            
            try
            {
                if (isSelected)
                {
                    securityGroupUserService.MapUsersToSecurityGroup(securityGroupUsers);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
                else
                {
                    FlashMessage1.Message = "No records are selected";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }
            }
            catch (Exception)
            {
                FlashMessage1.Message = "Saving Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }

            GetUnmappedUsers();
            GetMappedSecurityGroupUserBySecurityGroup();
        }

        protected void buttonRemoveLeft_Click(object sender, EventArgs e)
        {
            SecurityGroupUserService securityGroupUserService = new SecurityGroupUserService();
            long selectedSecGrpUsrIds = 0;
            Boolean isSelected = false;

            foreach (GridViewRow gridViewRow in gridViewMappedStakeholders.Rows)
            {
                if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                {
                    selectedSecGrpUsrIds =
                        securityGroupUserService.GetSecurityGroupUserIdFromSecurityGroupUser(
                            Convert.ToInt64(gridViewRow.Cells[1].Text),
                            Convert.ToInt64(dropDownListSecurityGroup.SelectedItem.Value));
                    isSelected = true;
                }
            }

            try
            {
                if (isSelected)
                {
                    securityGroupUserService.DeleteSecurityGroupUser(selectedSecGrpUsrIds);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
                else
                {
                    FlashMessage1.Message = "No records are selected";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

            }
            catch (Exception)
            {
                FlashMessage1.Message = "Saving Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }

            GetUnmappedUsers();
            GetMappedSecurityGroupUserBySecurityGroup();
        }

        protected void gridViewAllStakeholders_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void gridViewMappedStakeholders_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void checkBoxHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gridViewRow in ((GridView)((CheckBox)sender).Parent.Parent.Parent.Parent).Rows)
            {
                ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void gridViewAllStakeholders_SelectedIndexChanged(Object sender, EventArgs e)
        {
            GridViewRow row = gridViewAllStakeholders.SelectedRow;
            ViewState["UserId"] = row.Cells[1].Text;
            ViewState["Check"] = "Check";
        }
    }
}